package com.leruan.aasimp.action.newcar;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.bean.CarBasicCarTypeBean;
import com.leruan.aasimp.bean.CarBasicDemioTypesBean;
import com.leruan.aasimp.entity.AprioriEntity;
import com.leruan.aasimp.entity.CarBasicInfo;
import com.leruan.aasimp.entity.CarBasicInfoLog;
import com.leruan.aasimp.entity.CarDemioInfo;
import com.leruan.aasimp.service.BasicCarService;
import com.leruan.aasimp.util.Apriori;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CarBasicInfoAction extends ActionSupport {

	private static final long serialVersionUID = -3279640817461480148L;

	@Resource
	private BasicCarService basicCarService;

	// 报表显示
	public String showBb() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);

		List<CarBasicInfoLog> logs;
		try {
			logs = basicCarService.findAllLog();

			List<String> transSet = new ArrayList<String>();
			for (int i = 0; i < logs.size(); i++) {
				String idType = logs.get(i).getDemioTypes();
				transSet.add(idType);
			}
			// 设置权系数
			Map<String, Double> map = new HashMap<String, Double>();
			double a = 0;
			for (char c = 'a'; c <= 'z'; c++) {
				map.put(String.valueOf(c), 1.0 - a);
				a += 0.01;
			}
			a = 0;
			for (char c = 'A'; c <= 'Z'; c++) {
				map.put(String.valueOf(c), 1.0 - a);
				a += 0.02;
			}
			String[] transSet1 = new String[transSet.size()];
			for (int j = 0; j < transSet.size(); j++) {
				transSet1[j] = transSet.get(j);
			}
			Apriori aa = new Apriori(transSet1);
			List<AprioriEntity> allapriori = aa.run(0.55,0.2);
			for (int k = 0; k < allapriori.size(); k++) {
				String describe = allapriori.get(k).getDescribe();
				String[] split = describe.split("-->");
				List<String> list1 = new ArrayList<String>();
				for (int i = 0; i < split[0].length(); i++) {
					list1.add(split[0].substring(i, i + 1));
				}
				for (int i = 0; i < split[1].length(); i++) {
					list1.add(split[1].substring(i, i + 1));
				}
				// 计算权系数
				double s = 0;
				for (int i = 0; i < list1.size(); i++) {
					s += map.get(list1.get(i));
				}
				double weight = s / list1.size();
				allapriori.get(k).setWeightConfidence(
						weight * allapriori.get(k).getConfidence());
				Double count = 0.0;
				for (int i = 0; i < logs.size(); i++) {
					String idType = logs.get(i).getDemioTypes();
					boolean flag = true;
					for (int j = 0; j < list1.size(); j++) {
						if (!idType.contains(list1.get(j))) {
							flag = false;
						}
					}
					if (flag) {
						count++;
					}
				}
				allapriori.get(k).setSupport(count / logs.size());
				allapriori.get(k).setWeightSupport(
						weight * allapriori.get(k).getSupport());
			}
			String s = new String();
			for (int k = 0; k < allapriori.size(); k++) {
				String describe = allapriori.get(k).getDescribe();
				String[] strs = describe.split("-->");
				for (int j = 0; j < strs.length; j++) {
					if (strs[j].length() > 1) {
						char[] c = strs[j].toCharArray();
						for (int j1 = 0; j1 < c.length; j1++) {
							if (!s.contains(String.valueOf(c[j1]))) {
								s += c[j1];
							}
						}
					} else {
						if (!s.contains(strs[j])) {
							s += strs[j];
						}
					}
				}
			}
			request.getSession().setAttribute("demioTypes", s);
			request.getSession().setAttribute("allapriori", allapriori);

			List<CarBasicDemioTypesBean> demioList = basicCarService
					.getDemioTypesList();
			request.getSession().setAttribute("demioList", demioList);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showBb";
	}

	// 初始化车系首页
	public String showDemio() {

		Map<String, String> map = new HashMap<String, String>();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {

			String brand = request.getParameter("brand");
			String department = request.getParameter("department");

			if (brand != null & !"0".equals(brand)) {
//				brand = new String(brand.getBytes("iso-8859-1"), "UTF-8");
//				brand = URLDecoder.decode(brand, "utf-8");
				map.put("brand", brand);
				request.setAttribute("brand", brand);
			}
			if (department != null & !"0".equals(department)) {
//				department = new String(department.getBytes("iso-8859-1"),
//						"UTF-8");
//				department = URLDecoder.decode(department, "utf-8");
				map.put("department", department);
				request.setAttribute("department", department);
			}

			List<CarDemioInfo> carDemioList = basicCarService
					.findDemioList(map);
			request.setAttribute("carDemioList", carDemioList);

			List<String> brands = basicCarService.getBrandList();
			request.setAttribute("brands", brands);

			List<String> departments = basicCarService
					.getDepartmentListByBrand(brand);
			request.setAttribute("departments", departments);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showDemio";
	}

	public String showCarTypeIndex() {

		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("demioType", request.getParameter("demioType"));
		return "showCarTypeIndex";
	}

	// 显示车型页面
	public String showCarType() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String demioType = request.getParameter("demioType");
		
		try {
			if (demioType != null) {
//				demioType = new String(demioType.getBytes("iso-8859-1"),
//						"UTF-8");
//				demioType = URLDecoder.decode(demioType, "utf-8");
				request.setAttribute("demioType", demioType);
			}
			String carType = request.getParameter("carType");
			if (carType != null && !"0".equals(carType)) {
//				carType = new String(carType.getBytes("iso-8859-1"),
//						"UTF-8");
//				carType = URLDecoder.decode(carType, "utf-8");
				request.setAttribute("carType", carType);
			}else{
				carType = null;
			}
			List<CarBasicCarTypeBean> carTypes = basicCarService
					.getCarTypes(demioType, carType);
			request.setAttribute("carTypes", carTypes);
			
			List<CarBasicCarTypeBean> carTypesTotal = basicCarService
			.getCarTypes(demioType, null);
			
			request.setAttribute("carTypesTotal", carTypesTotal);
			
			String demioTypes = (String) request.getSession().getAttribute("demioTypes");
			if(demioTypes != null && !"".equals(demioTypes.trim()) && demioTypes.contains(demioType)){
				List<CarDemioInfo> dList = basicCarService.getCarDemioInfoList(demioTypes);
				request.setAttribute("dList", dList);
			}
			
			CarDemioInfo carDemioInfo = basicCarService.getCarDemioInfo(demioType);
			request.setAttribute("carDemioInfo", carDemioInfo);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "showCarType";
	}
	
	public String showDetailIndex(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("id", request.getParameter("id"));
		return "showDetailIndex";
	}
	
	public String showDetail(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		try {
			CarBasicInfo carBasicInfoDetail = basicCarService.getCarBasicInfoById(id);
			request.setAttribute("carBasicInfoDetail", carBasicInfoDetail);
			
			List<String> imagesUrl = basicCarService.getImagesUrlById(id);
			request.setAttribute("imagesUrl", imagesUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showDetail";
		
	}
	
	public void setBasicCarService(BasicCarService basicCarService) {
		this.basicCarService = basicCarService;
	}

}
